package ui.activity.mine.presenter;


import com.google.gson.Gson;

import net.ck.hzq.TobuyApplication;

import java.util.List;

import javax.inject.Inject;

import Bean.ForwardParamBean;
import Bean.GetBankcardBean;
import Utils.CheckPhoneNumberUtils;
import Utils.StringUtil;
import base.BaseBiz;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import ui.activity.home.PickupMachineAct;
import ui.activity.mine.BankCardAct;
import ui.activity.mine.PayPwdAct;
import ui.activity.mine.PresentRecordAct;
import ui.activity.mine.biz.ReturnIncomeBiz;
import ui.activity.mine.contract.ReturnIncomeContract;
import ui.activity.poscontrol.beanmodel.TiXianResultBean;
import ui.model.AccountMoneyInfo;
import ui.model.ItemsInfo;
import ui.model.WithDrawInfo;

/**
 * Created by chenjian on 19/2/26
 */

public class ReturnIncomePresenter implements ReturnIncomeContract.Presenter {

    ReturnIncomeContract.View view;
    ReturnIncomeBiz returnIncomeBiz;
    AccountMoneyInfo dataBean;


    @Inject
    public ReturnIncomePresenter(ReturnIncomeContract.View view) {
        this.view = view;
    }


    @Override
    public void setBiz(BaseBiz biz) {
        returnIncomeBiz = (ReturnIncomeBiz) biz;
    }


    @Override
    public void getUserCard() {
        view.loading(true);
        returnIncomeBiz.getCardList(new BaseBiz.Callback<GetBankcardBean.DataBean>() {
            @Override
            public void onSuccess(GetBankcardBean.DataBean info) {
                if (info.getItems().size() > 0) {
                    for (int i = 0; i < info.getItems().size(); i++) {
                        if (info.getItems().get(i).getDefaultCard() == 1) {
                            view.upDateUI(info.getItems().get(i));
                        }
                    }
                }
                view.loading(false);
            }

            @Override
            public void onFailure(GetBankcardBean.DataBean info) {
                view.loading(false);

            }
        });
    }

    public void getMoneyInfo() {
        view.loading(true);
        returnIncomeBiz.getUserIncome(new BaseBiz.Callback<ItemsInfo<AccountMoneyInfo>>() {
            @Override
            public void onSuccess(ItemsInfo<AccountMoneyInfo> info) {
                dataBean = info.getItems();
                view.upDateMoneyUI(info.getItems());
                view.loading(false);
            }

            @Override
            public void onFailure(ItemsInfo<AccountMoneyInfo> info) {
                view.loading(false);
            }
        });
    }

    @Override
    public void toWithDraw(String money, String carNumber, String cardId, String cardName, String type, String code) {
        try {
            if (StringUtil.isNullOrEmpty(carNumber)) {
                view.showMsg("请选择银行卡!");
                return;
            }
            if (StringUtil.isNullOrEmpty(money)) {
                view.showMsg("请输入提现金额!");
                return;
            }
            if (!CheckPhoneNumberUtils.isNumber(money)) {
                view.showMsg("请输入纯数字!");
                return;
            }
//            if (Double.parseDouble(money) < 10) {
//                view.showMsg("提现金额不能小于10元!");
//                return;
//            }
//            if (Double.parseDouble(money) % 10 > 0) {
//                view.showMsg("提现金额必须为10元的整数倍!");
//                return;
//            }
            if (type.equals("1")) {
                Double a = Double.parseDouble(money);
                Double b = Double.parseDouble(StringUtil.getFormatPrice(dataBean.getDividedAvailableAmount()));
                if (a > b) {
                    view.showMsg("提现金额不能超过可提现金额!");
                    return;
                }
            } else if (type.equals("2")) {
                Double a = Double.parseDouble(money);
                Double b = Double.parseDouble(StringUtil.getFormatPrice(dataBean.getReturnAvailableAmount()));
                if (a > b) {
                    view.showMsg("提现金额不能超过可提现金额!");
                    return;
                }
            }
            view.loading(true);
            ForwardParamBean f = new ForwardParamBean();
            f.setAmount(money);
            f.setCardId(cardId);
            f.setType(type);
            f.setSecret(code);
            f.setVersion("1");
            RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(f));
            returnIncomeBiz.setIncome(requestBody, new BaseBiz.Callback<List<TiXianResultBean>>() {
                @Override
                public void onSuccess(List<TiXianResultBean> info) {

                    TobuyApplication.resultBeans = info;
                    view.successJump(money, cardName);
                    view.loading(false);
                    view.setEnable(true);
                }

                @Override
                public void onFailure(List<TiXianResultBean> info) {
                    view.loading(false);
                    view.setEnable(true);
                }
            });
        } catch (Exception e) {

        }
    }

    @Override
    public void toShowInfo() {
        view.ShowDialog();
    }

    @Override
    public void toCardList() {
        view.startActString(BankCardAct.class, "1");
    }

    @Override
    public void toWithDrawList() {
        view.startAct(PresentRecordAct.class, null);
    }

    @Override
    public void toBuy() {
        view.startAct(PickupMachineAct.class, null);
    }

    @Override
    public void getCode(String money, String carNumber, String cardId, String cardName, String type) {
        try {
            if (StringUtil.isNullOrEmpty(carNumber)) {
                view.showMsg("请选择银行卡!");
                return;
            }
            if (StringUtil.isNullOrEmpty(money)) {
                view.showMsg("请输入提现金额!");
                return;
            }
            if (!CheckPhoneNumberUtils.isNumber(money)) {
                view.showMsg("请输入纯数字!");
                return;
            }
//            if (Double.parseDouble(money) < 10) {
//                view.showMsg("提现金额不能小于10元!");
//                return;
//            }
//            if (Double.parseDouble(money) % 10 > 0) {
//                view.showMsg("提现金额必须为10元的整数倍!");
//                return;
//            }
            if (type.equals("1")) {
                Double a = Double.parseDouble(money);
                Double b = Double.parseDouble(StringUtil.getFormatPrice(dataBean.getDividedAvailableAmount()));
                if (a > b) {
                    view.showMsg("提现金额不能超过可提现金额!");
                    return;
                }
            } else if (type.equals("2")) {
                Double a = Double.parseDouble(money);
                Double b = Double.parseDouble(StringUtil.getFormatPrice(dataBean.getReturnAvailableAmount()));
                if (a > b) {
                    view.showMsg("提现金额不能超过可提现金额!");
                    return;
                }
            }
            view.loading(true);
            view.showEdit();
            returnIncomeBiz.yuPay(new BaseBiz.Callback<Object>() {
                @Override
                public void onSuccess(Object info) {
                    view.showMsg("发送验证码成功");
                    view.loading(false);
                }

                @Override
                public void onFailure(Object info) {
                    view.loading(false);
                }
            });
        } catch (Exception e) {

        }
    }

    @Override
    public void getPwdInfo(String money, String carNumber, String cardId, String cardName, String type) {
        try {
            if (StringUtil.isNullOrEmpty(carNumber)) {
                view.showMsg("请选择银行卡!");
                return;
            }
            if (StringUtil.isNullOrEmpty(money)) {
                view.showMsg("请输入提现金额!");
                return;
            }
            if (!CheckPhoneNumberUtils.isNumber(money)) {
                view.showMsg("请输入纯数字!");
                return;
            }
//            if (Double.parseDouble(money) < 10) {
//                view.showMsg("提现金额不能小于10元!");
//                return;
//            }
//            if (Double.parseDouble(money) % 10 > 0) {
//                view.showMsg("提现金额必须为10元的整数倍!");
//                return;
//            }
            if (type.equals("1")) {
                Double a = Double.parseDouble(money);
                Double b = Double.parseDouble(StringUtil.getFormatPrice(dataBean.getDividedAvailableAmount()));
                if (a > b) {
                    view.showMsg("提现金额不能超过可提现金额!");
                    return;
                }
            } else if (type.equals("2")) {
                Double a = Double.parseDouble(money);
                Double b = Double.parseDouble(StringUtil.getFormatPrice(dataBean.getReturnAvailableAmount()));
                if (a > b) {
                    view.showMsg("提现金额不能超过可提现金额!");
                    return;
                }
            }
            view.loading(true);
            returnIncomeBiz.getPwd(new BaseBiz.Callback<Boolean>() {
                @Override
                public void onSuccess(Boolean info) {
                    if (info) {
                        view.showPwd();
                    } else {
                        view.showAddPwd();
                    }
                    view.loading(false);
                }

                @Override
                public void onFailure(Boolean info) {
                    view.loading(false);
                }
            });
        } catch (Exception e) {

        }

    }

    @Override
    public void toAddPwd() {
        view.startAct(PayPwdAct.class, null);
    }

    @Override
    public void getWithDraw(boolean isRefresh) {
        returnIncomeBiz.getWithDrawInfo(new BaseBiz.Callback<WithDrawInfo>() {
            @Override
            public void onSuccess(WithDrawInfo info) {
                view.upDateInfo(info);
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
                view.hasLoadMore(false);
            }

            @Override
            public void onFailure(WithDrawInfo info) {
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
                view.hasLoadMore(false);
            }
        });
    }
}
